<?php
namespace Index\Controller;
use Think\Controller;
class SearchController extends Controller {
    //课程搜索
    public function classAction(){
    	
        $uid = I('session.uid');
        $Model = M();

        $sql = "select username from user where id ='{$uid}'";
        $userInfo = $Model->query($sql);
        $userInfo = $userInfo[0];
        $this->assign('userInfo',$userInfo);

        //全部课程分类
        $sql = "select * from category";
        $category = $Model->query($sql);
        $cate= $this->_fibTree($category,0);
        $this->assign('cate',$cate);

        //一级分类
        $fid = empty(I('get.fid')) ? 'all' : I('get.fid');
        $this->assign('fid',$fid);
        $sql = "select * from category where id = '{$fid}'";
        $firstCate = $Model->query($sql);
        $firstCate = $firstCate[0];
        $this->assign('firstCate',$firstCate);

        $sql = "select * from category where fid = 0";
        $categoryInfo = $Model->query($sql);
        $this->assign('categoryInfo',$categoryInfo);

        //二级分类
        $sid = empty(I('get.sid')) ? 'all' : I('get.sid');
        $this->assign('sid',$sid);
        $sql = "select * from category where id = '{$sid}'";
        $secondCate = $Model->query($sql);
        $secondCate = $secondCate[0];
        $this->assign('secondCate',$secondCate);

        if($fid != 'all'){
            $sql = "select * from category where fid = '{$fid}'";
            $category = $Model->query($sql);
            $this->assign('category',$category);
        }

        $time = time();
        $sql = "select t1.categoryid,t1.id,t1.coursename,t1.picture,t1.introduction,t1.price,t2.starttime,t2.endtime,t3.teachername,t4.apply,ifnull(t4.apply,0) applynum from course t1 left join courselist t2 on t1.id = t2.courseid left join teacher t3 on t1.id = t3.courseid left join (select courseid,count(*) apply from application group by courseid) t4 on t1.id = t4.courseid ";
        if($_GET){
            $sql .= ' where ';
            // $tempStr = '';
            if($fid != 'all' && $sid == 'all'){
                $sonId = $this->_getSonId($fid);
                $sonId.= 0;
                $sql .= "t1.categoryid in ($sonId)";     
            }
            if($fid != 'all' && $sid != 'all'){
                $sql .= "t1.categoryid = $sid";
            }
        }
        $resData = 1;
        

        if($_POST){
            $keyword = I('post.keyword');
            if(!empty($keyword)){
                $num = 5;
                import("Vendor.Pscws.pscws4");
                $pscws = new \PSCWS4();
                $pscws->PSCWS4('utf8');
                $pscws->set_dict(CONF_PATH . 'etc/dict.utf8.xdb');
                $pscws->set_rule(CONF_PATH . 'etc/rules.utf8.ini');
                $pscws->set_ignore(true);
                $pscws->send_text($keyword);
                $words = $pscws->get_tops($num);
                $pscws->close();
                
                if(empty($words)){
                    $resData = 0;               
                    
                }else{
                    $sql .= ' where concat(t1.coursename,t1.introduction)';
                    foreach($words as $val) {
                        $sql .= " like "."'%".$val['word']."%' or ";
                    }
                    $sql = substr($sql,0,-4);
                    $resData = 2;
                }
            }
        }
        $this->assign('keyword',$keyword);
        $this->assign('resData',$resData);
        $sql.=" and t2.starttime > '{$time}' group by t2.courseid";
        $data = $Model->query($sql);
        //分页
        $count = count($data);
        $this->assign('count',$count);
        $Page = new \Think\Page($count,6);
        $show = $Page->show();
        
        $sql.=" limit {$Page->firstRow},{$Page->listRows}";
        $data = $Model->query($sql);
        $this->assign('data',$data);
        $this->assign('page',$show);

       

    	$this->display('class');
    }

    
    //循环获取某个分类下的子分类
    private function _getSonId($fid){
        // $str = '';
        $Model = M();
        $sql = "select * from category where fid = '{$fid}' ";
        $sonArr = $Model->query($sql);
        foreach ($sonArr as $key => $value) {
            $str .= $value['id'];
            $str .= ",";
            $str .= $this->_getSonId($value['id']);
        }
        return $str;
    }   

    //全部课程分类调用方法
    private function _fibTree($arr, $fid = 0) {
        $tree = array();
        $temp = array();
        if($arr) {
            foreach($arr as $k=>$v) {
                if($v['fid'] == $fid) {
                    $temp = $this->_fibTree($arr, $v['id']);
                    $temp AND $v['son'] = $temp;
                    $tree[] = $v;
                }
            }
        }
        return $tree;
    }   

    //老师搜索
      public function teacherAction(){
        
        $uid = I('session.uid');
        $Model = M();

        $sql = "select username from user where id ='{$uid}'";
        $userInfo = $Model->query($sql);
        $userInfo = $userInfo[0];
        $this->assign('userInfo',$userInfo);

        //全部课程分类
        $sql = "select * from category";
        $category = $Model->query($sql);
        $cate= $this->_fibTree($category,0);
        $this->assign('cate',$cate);

        //一级分类
        $fid = empty(I('get.fid')) ? 'all' : I('get.fid');
        $this->assign('fid',$fid);
        $sql = "select * from category where id = '{$fid}'";
        $firstCate = $Model->query($sql);
        $firstCate = $firstCate[0];
        $this->assign('firstCate',$firstCate);

        $sql = "select * from category where fid = 0";
        $categoryInfo = $Model->query($sql);
        $this->assign('categoryInfo',$categoryInfo);

        //二级分类
        $sid = empty(I('get.sid')) ? 'all' : I('get.sid');
        $this->assign('sid',$sid);
        $sql = "select * from category where id = '{$sid}'";
        $secondCate = $Model->query($sql);
        $secondCate = $secondCate[0];
        $this->assign('secondCate',$secondCate);

        if($fid != 'all'){
            $sql = "select * from category where fid = '{$fid}'";
            $category = $Model->query($sql);
            $this->assign('category',$category);
        }


        $sql = "select *,sum(t6.apply) as applynum,count(t6.courseid) as coursenum from (select t1.id,t1.teachername,t1.teacherface,t1.skill,t1.brief,t4.*,t5.apply from teacher as t1 left join (select t2.id as courseid,t2.categoryid from course as t2 left join category as t3 on t2.categoryid = t3.id) as t4 on t1.courseid = t4.courseid left join (select courseid,count(*) as apply from application group by courseid) as t5 on t1.courseid = t5.courseid) as t6";
        if($_GET){
            $sql .= ' where ';
            // $tempStr = '';
            if($fid != 'all' && $sid == 'all'){
                $sonId = $this->_getSonId($fid);
                $sonId.= 0;
                $sql .= "t6.categoryid in ($sonId)";     
            }
            if($fid != 'all' && $sid != 'all'){
                $sql .= "t6.categoryid = $sid";
            }
        }
        $resData = 1;
        

        if($_POST){
            $keyword = I('post.keyword');
            if(!empty($keyword)){
                $num = 5;
                import("Vendor.Pscws.pscws4");
                $pscws = new \PSCWS4();
                $pscws->PSCWS4('utf8');
                $pscws->set_dict(CONF_PATH . 'etc/dict.utf8.xdb');
                $pscws->set_rule(CONF_PATH . 'etc/rules.utf8.ini');
                $pscws->set_ignore(true);
                $pscws->send_text($keyword);
                $words = $pscws->get_tops($num);
                $pscws->close();
                
                if(empty($words)){
                    $resData = 0;               
                    
                }else{
                    $sql .= ' where concat(t6.teachername,t6.skill,t6.brief)';
                    foreach($words as $val) {
                        $sql .= " like "."'%".$val['word']."%' or ";
                    }
                    $sql = substr($sql,0,-4);
                    $resData = 2;
                }
            }
            
        }
        $this->assign('keyword',$keyword);
        $this->assign('resData',$resData);
        $sql.=" group by t6.teachername";
        $data = $Model->query($sql);
        //分页
        $count = count($data);
        $Page = new \Think\Page($count,6);
        $show = $Page->show();       
        $sql.=" limit {$Page->firstRow},{$Page->listRows}";
        $data = $Model->query($sql);
        $this->assign('data',$data);

        $this->assign('page',$show);

        $this->display('teacher');
    }

    //
    public function indexAction(){

    }

}